package com.darkplanet.management.service;

import com.darkplanet.management.dao.EmployeeRoleDaoImp;
import com.darkplanet.management.dao.RoleMenuDaoimpl;
import com.darkplanet.management.dao.SecondMenuDaoimpl;
import com.darkplanet.management.domain.EmployeeRole;
import com.darkplanet.management.domain.FirstMenu;
import com.darkplanet.management.domain.RoleMenu;
import com.darkplanet.management.domain.SecondMenu;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EmployeeMenuImp implements IEmployeeMenu {

    @Override
    public List<EmployeeRole> getRoles(Integer employee_id) {
        EmployeeRoleDaoImp employeeRoleDao = new EmployeeRoleDaoImp();
        List<EmployeeRole> employeeRoles = employeeRoleDao.getEmployeeRolesById(employee_id);
        return employeeRoles;
    }

    @Override
    public List<SecondMenu> getSecondMenus(Integer employee_id) {
        List<EmployeeRole> roles = getRoles(employee_id);
        RoleMenuDaoimpl roleMenuDao = new RoleMenuDaoimpl();
        SecondMenuDaoimpl secondMenuDao= new SecondMenuDaoimpl();
        Set<Integer> fsecondMenus = new HashSet<>();
        List<SecondMenu> secondMenus = new ArrayList<SecondMenu>();
        for (EmployeeRole role : roles) {
            List<RoleMenu> menus = roleMenuDao.getRoles(role.getRole_id());
            for (RoleMenu menu : menus) {
                SecondMenu secondMenu = secondMenuDao.getSecondMenuById(menu.getS_menu_id());
                if (fsecondMenus.add(secondMenu.getS_menu_id())){
                    secondMenus.add(secondMenu);
                }
            }
            }
        return secondMenus;
    }

    @Override
    public List<List> getMenus(Integer employee_id) {
        FirstMenuServiceImp firstMenuService = new FirstMenuServiceImp();
        List<SecondMenu> fsecondMenus = getSecondMenus(employee_id);
        List<SecondMenu> secondMenus = sort(fsecondMenus);
        List<FirstMenu> firstMenus = new ArrayList<>() ;
        List<List> menuList = new ArrayList<>();
        HashSet<Integer> fFirstMenu = new HashSet<>();
        for (SecondMenu secondMenu : secondMenus) {
            Integer f_menu_id = secondMenu.getF_menu_id();
            if(fFirstMenu.add(f_menu_id)){
                firstMenus.add(firstMenuService.getFirstMenuById(f_menu_id));
            }
        }
        menuList.add(firstMenus);
        menuList.add(secondMenus);
        return menuList;
    }

    @Override
    public List<SecondMenu> sort(List<SecondMenu> list) {
        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size()-1 ; j > i; j--) {
                if(list.get(i).getS_menu_id()>list.get(j).getS_menu_id()){
                    SecondMenu temp1 = list.get(i);
                    SecondMenu temp2 = list.get(j);
                    list.add(i+1,temp2);
                    list.remove(i);
                    list.add(j+1,temp1);
                    list.remove(j);
                }
            }
        }
        return list;
    }
}
